package com.cms.admin.action.healthrecord;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.GoodsDataUserRemark;
import com.cms.admin.model.GoodsDataUserRemarkExt;
import com.cms.admin.model.PageBean;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.healthrecord.GoodsDataUserRemarkService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class GoodsDataUserRemarkAction extends BaseAction implements ModelDriven<GoodsDataUserRemarkExt>{


    private TemplateService templateService;
    private GoodsDataUserRemarkService goodsDataUserRemarkService;
    private GoodsDataUserRemarkExt search=new GoodsDataUserRemarkExt();
    private PageBean page;

    public String ajaxQueryGoodsDataUserRemarks()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        page = new PageBean(search.getCurrentPage(), search.getPageSize());

        List<GoodsDataUserRemark> goodsDataUserRemarkList=goodsDataUserRemarkService.queryGoodsDataUserRemarkList(page,search);
        String res = null;
        try {
            res = getQueryResult(goodsDataUserRemarkList, page);
            JSONObject o = new JSONObject();
            o.put("pageCount", page.getTotalPage());
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            LOG.error("ajaxQueryUnGoingOrder", e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    private String getQueryResult(List<GoodsDataUserRemark> goodsDataUserRemarkList, PageBean page)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("goodsDataUserRemarkList", goodsDataUserRemarkList);
        map.put("page", page);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_HEALTH_RECORDS_USERREMARK_QUERY_RESULT, map);
        return result;
    }




    public String ajaxDeleteGoodsDataUserRemark()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        goodsDataUserRemarkService.deleteGoodsDataUserRemark(search.getRecid());
        JSONObject o = new JSONObject();
        o.put("errorCode", 0);
        o.put("errorMsg", "success");
        o.put("recid", search.getRecid());
        return ajaxJsonSuccessMessage(o);
    }

    public String ajaxAddGoodsDataUserRemark(){

        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:ajaxAddGoodsDataUserRemark" );

        goodsDataUserRemarkService.addGoodsDataUserRemark(search);

        JSONObject o = new JSONObject();
        if (search.getRecid()>0)
        {
            o.put("errorCode", 0);
            o.put("errorMsg", "success");
        }else
        {
            o.put("errorCode", -1);
            o.put("errorMsg", "新增问答记录失败");
        }
        return ajaxJsonSuccessMessage(o);
    }

    public String ajaxQueryGoodsDataUserRemarkForUpdate()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:ajaxAddGoodsDataUserRemark" );
        GoodsDataUserRemark remark=goodsDataUserRemarkService.queryGoodsDataUserRemark(search.getRecid());
        if (null!=remark)
        {
            try {
                String res=getQueryResultForUpdate(remark);

                JSONObject o = new JSONObject();
                o.put("errorCode", 0);
                o.put("errorMsg", "success");
                o.put("content",res);
                return ajaxJsonSuccessMessage(o);

            } catch (Exception e) {
                LOG.error("ajaxQueryGoodsDataUserRemarkForUpdate", e);
                JSONObject o = new JSONObject();
                o.put("errorCode", -1);
                o.put("errorMsg", Constants.INFO_SYS_ERROR);
                return ajaxJsonSuccessMessage(o);
            }
        }else
        {
            JSONObject o = new JSONObject();
            o.put("errorCode", -1);
            o.put("errorMsg", "该条记录不存在");
            return ajaxJsonSuccessMessage(o);
        }
    }

    private String getQueryResultForUpdate(GoodsDataUserRemark goodsDataUserRemark)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("goodsDataUserRemark", goodsDataUserRemark);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_HEALTH_RECORDS_USERREMARK_UPDATE_QUERY_RESULT, map);
        return result;
    }

    public String ajaxUpdateGoodsDataUserRemark()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:ajaxAddGoodsDataUserRemark" );
        search.setUpdateDate(new Date());
        goodsDataUserRemarkService.updateGoodsDataUserRemark(search);
        JSONObject o = new JSONObject();
        o.put("errorCode", 0);
        o.put("errorMsg", "success");
        return ajaxJsonSuccessMessage(o);
    }

    @Override
    public GoodsDataUserRemarkExt getModel() {
        return search;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public GoodsDataUserRemarkService getGoodsDataUserRemarkService() {
        return goodsDataUserRemarkService;
    }

    public void setGoodsDataUserRemarkService(GoodsDataUserRemarkService goodsDataUserRemarkService) {
        this.goodsDataUserRemarkService = goodsDataUserRemarkService;
    }

    public GoodsDataUserRemarkExt getSearch() {
        return search;
    }

    public void setSearch(GoodsDataUserRemarkExt search) {
        this.search = search;
    }
}
